#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/wait.h>
void error_out(const char *msg)
{
	perror(msg);
	exit(EXIT_FAILURE);
}
int main (int argc, char *argv[])
{
    int r;
    const char *memname = "/mymem";
    const size_t region_size = sysconf(_SC_PAGE_SIZE);
    int fd = shm_open(memname, O_CREAT|O_TRUNC|O_RDWR, 0666);
    if (fd == -1)
        error_out("shm_open");
    r = ftruncate(fd, region_size);
    if (r != 0)
        error_out("ftruncate");
    void *ptr = mmap(0, region_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == MAP_FAILED)
        error_out("MMAP");
    close(fd);
    pid_t pid = fork();
    if (pid == 0)
    {
        u_long *d = (u_long *)ptr;
        *d = 0xdeadbeef;
        exit(0);
    }
    else
    {
        int status;
        waitpid(pid, &status, 0);
        printf("child wrote %#lx\n", *(u_long *)ptr);
    }
    sleep(50);
    r = munmap(ptr, region_size);
    if (r != 0)
        error_out("munmap");
    r = shm_unlink(memname);
    if (r != 0)
        error_out("shm_unlink");
    return 0;
}